package com.kt.thread.demo.model;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Description:
 * @Author: kt
 * @CreateTime: 2021/3/13
 */
public class ModelTest {
    public static void main(String[] args) {
        Server server = new Server();
        int count = 10;
        Client[] clients = new Client[count];
        for (int i = 0; i < count; i++) {
            clients[i] = new Client("client" + i, server);
        }
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                ThreadLocalRandom random = ThreadLocalRandom.current();
                while (true) {
                    int i1 = random.nextInt(count);
                    int i2 = Math.min(i1, i1 + 1);
                    int i3 = Math.max(0, i1 - 1);
                    clients[i1].send(new WriteCommand("key" + i1, UUID.randomUUID().toString()));
                    clients[i2].send(new ReadCommand("key" + i2));
                    clients[i3].send(new WriteCommand("key" + i3, UUID.randomUUID().toString()));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                ThreadLocalRandom random = ThreadLocalRandom.current();
                int i1 = random.nextInt(count);
                int i2 = Math.min(i1, i1 + 1);
                int i3 = Math.max(0, i1 - 1);
                clients[i1].send(new WriteCommand("key" + i1, UUID.randomUUID().toString()));
                clients[i2].send(new ReadCommand("key" + i2));
                clients[i3].send(new ReadCommand("key" + i3));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        server.start();
        thread1.start();
        thread2.start();
    }
}
